package com.etl.dataflow.scheduler.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etl.dataflow.common.exception.ServiceException;
import com.etl.dataflow.common.response.R;
import com.etl.dataflow.scheduler.admin.entity.DataflowTaskGroupInfo;
import com.etl.dataflow.scheduler.admin.entity.DataflowTaskInfo;
import com.etl.dataflow.scheduler.admin.entity.request.PageRequest;
import com.etl.dataflow.scheduler.admin.mapper.DataflowTaskGroupInfoMapper;
import com.etl.dataflow.scheduler.admin.mapper.DataflowTaskInfoMapper;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * * <p>
 * * 任务组信息 服务实现类
 * * </p>
 *
 * @author dx
 * @since 2022/6/20
 */
@Service
public class DataflowTaskGroupInfoService extends ServiceImpl<DataflowTaskGroupInfoMapper, DataflowTaskGroupInfo>
        implements DefaultService<DataflowTaskGroupInfo> {

    private final DataflowTaskInfoMapper taskInfoMapper;

    private final DataflowTaskInfoService taskInfoService;

    public DataflowTaskGroupInfoService(DataflowTaskInfoMapper taskInfoMapper, DataflowTaskInfoService taskInfoService) {
        this.taskInfoMapper = taskInfoMapper;
        this.taskInfoService = taskInfoService;
    }

    public Page<? extends DataflowTaskGroupInfo> page(PageRequest pageRequest) {
        return super.baseMapper
                .selectPage(toPage(pageRequest), orLikeAllFields(pageRequest, DataflowTaskGroupInfo.class, "id"));
    }

    public R search(String keyword) {
        QueryWrapper<DataflowTaskGroupInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "%" + keyword + "%");
        List<DataflowTaskGroupInfo> datasourceList = super.baseMapper.selectList(queryWrapper);
        return R.success().put("data", datasourceList);
    }

    public R runGroup(Long groupId) {
        List<DataflowTaskInfo> taskInfos = queryTaskInfosByGroupId(groupId);
        List<String> failedTasks = new ArrayList<>();
        for (DataflowTaskInfo taskInfo : taskInfos) {
            try {
                taskInfoService.runTask(taskInfo.getId());
            } catch (ServiceException e) {
                log.error(e.getMsg());
                failedTasks.add(taskInfo.getName());
            }
        }
        if (failedTasks.isEmpty()) {
            return R.success();
        }
        return R.error().put("failedTasks", failedTasks);
    }

    public R enableGroupTimer(Long groupId) {
        List<DataflowTaskInfo> taskInfos = queryTaskInfosByGroupId(groupId);
        Integer timerState;
        for (DataflowTaskInfo taskInfo : taskInfos) {
            timerState = taskInfo.getTimerState();
            try {
                if (timerState == null || timerState == DataflowTaskInfo.TimerState.DISABLED.ordinal()) {
                    taskInfoService.enableTimer(taskInfo.getId());
                }
            } catch (ServiceException | ParseException ignored) {
            }
        }
        return R.success();
    }

    public R disableGroupTimer(Long groupId) {
        List<DataflowTaskInfo> taskInfos = queryTaskInfosByGroupId(groupId);
        Integer timerState;
        for (DataflowTaskInfo taskInfo : taskInfos) {
            timerState = taskInfo.getTimerState();
            try {
                if (timerState == null || timerState == DataflowTaskInfo.TimerState.ENABLED.ordinal()) {
                    taskInfoService.disableTimer(taskInfo.getId());
                }
            } catch (ServiceException ignored) {
            }
        }
        return R.success();
    }

    private List<DataflowTaskInfo> queryTaskInfosByGroupId(Long groupId) {
        QueryWrapper<DataflowTaskInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id", groupId);
        return taskInfoMapper.selectList(queryWrapper);
    }
}
